package com.caojing.haolemai.httputils;

import android.util.Log;

import com.caojing.haolemai.bean.ContentBean;
import com.caojing.haolemai.bean.HomeLvBean;
import com.caojing.haolemai.bean.HomeVpBean;
import com.caojing.haolemai.bean.OutletsSharefragmentBean;
import com.caojing.haolemai.bean.ProductBean;
import com.caojing.haolemai.bean.categoryclickbean.CategoryClickBean;
import com.caojing.haolemai.bean.outletsdetailsbean.OutletsDeatailsBean;
import com.caojing.haolemai.bean.outletsproductbean.OutletsProductBean;
import com.caojing.haolemai.bean.tabBean;
import com.caojing.haolemai.bean.SearchHintBean;
import com.caojing.haolemai.bean.categorybean.CategoryBean;

import java.util.HashMap;

import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class HttpUtils {

    private static HttpUtils instance;

    private HttpUtils() {
    }

    public static synchronized HttpUtils getInstance() {
        if (instance == null) {
            instance = new HttpUtils();
        }
        return instance;
    }

    public interface HttpCallBack<T> {
        void onSucess(T t);

        void onFail();
    }

    public void getHomeVpBean(final HttpCallBack<HomeVpBean> homeVpBeanHttpCallBack) {
        Call<HomeVpBean> homeVpBeanCall = RetrofitHelper
                .getInstance()
                .createRetrofitService(IHomeService.class)
                .getHomeVpBean();
        homeVpBeanCall.enqueue(new Callback<HomeVpBean>() {
            @Override
            public void onResponse(Call<HomeVpBean> call, Response<HomeVpBean> response) {
                HomeVpBean homeVpBean = response.body();
                homeVpBeanHttpCallBack.onSucess(homeVpBean);
            }

            @Override
            public void onFailure(Call<HomeVpBean> call, Throwable t) {
                homeVpBeanHttpCallBack.onFail();
            }
        });
    }

    public void getHomeLvBean(final HttpCallBack<HomeLvBean> homeLvBeanHttpCallBack) {
        Call<HomeLvBean> homeLvBeanCall = RetrofitHelper
                .getInstance()
                .createRetrofitService(IHomeService.class)
                .getHomeLvBean();
        homeLvBeanCall.enqueue(new Callback<HomeLvBean>() {
            @Override
            public void onResponse(Call<HomeLvBean> call, Response<HomeLvBean> response) {
                HomeLvBean homeLvBean = response.body();
                homeLvBeanHttpCallBack.onSucess(homeLvBean);
            }

            @Override
            public void onFailure(Call<HomeLvBean> call, Throwable t) {
                homeLvBeanHttpCallBack.onFail();
            }
        });
    }

    //这是分类的获取数据方法
    public void getCategoryBean(final HttpCallBack<CategoryBean> categoryBeanHttpCallBack) {

        Call<CategoryBean> categoryBeanCall = RetrofitHelper
                .getInstance()
                .createRetrofitService(IHomeService.class)
                .getCategoryBean();
        categoryBeanCall.enqueue(new Callback<CategoryBean>() {
            @Override
            public void onResponse(Call<CategoryBean> call, Response<CategoryBean> response) {
                //判断是否得到数据
                CategoryBean categoryBean = response.body();
//                    Log.i("TAG","得到分类的数据————————"+categoryBean);
                categoryBeanHttpCallBack.onSucess(categoryBean);

            }

            @Override
            public void onFailure(Call<CategoryBean> call, Throwable t) {
                // Log.i("TAG", "得到错误的数据————————" + t.getMessage());
                categoryBeanHttpCallBack.onFail();
            }
        });

    }

    //这是点击分类的获取数据方法
    public void getCategoryClickBean(HashMap<String, String> params, final HttpCallBack<CategoryClickBean> categoryClickBeanHttpCallBack) {

        Call<CategoryClickBean> categoryBeanCall = RetrofitHelper
                .getInstance()
                .createRetrofitService(IHomeService.class)
                .getCategoryClickBean(params);
        categoryBeanCall.enqueue(new Callback<CategoryClickBean>() {
            @Override
            public void onResponse(Call<CategoryClickBean> call, Response<CategoryClickBean> response) {
                //判断是否得到数据
                CategoryClickBean categoryClickBean = response.body();
                categoryClickBeanHttpCallBack.onSucess(categoryClickBean);
            }

            @Override
            public void onFailure(Call<CategoryClickBean> call, Throwable t) {
//                Log.i("TAG","得到错误的数据————————"+t.getMessage());
                categoryClickBeanHttpCallBack.onFail();
            }
        });

    }


    //Todo: 显示限时特卖的tablayout的数据
    public void getTabVerticalTabBean(final Map<String, String> params, Callback<tabBean> callback) {
        Call<tabBean> tabBeanCall = RetrofitHelper
                .getInstance()
                .createRetrofitService(IHomeService.class)
                .getTabBean(params);
        tabBeanCall.enqueue(callback);

    }

    //Todo: 显示限时特卖的每个fragment的数据
    public void getFragmentInnerResultBean(String pager, final Map<String, String> params, Callback<OutletsSharefragmentBean> callback) {
        Call<OutletsSharefragmentBean> outletsCountsResultsEntityCall = RetrofitHelper
                .getInstance()
                .createRetrofitService(IHomeService.class)
                .getFragmentInnerBean(pager, params);
        outletsCountsResultsEntityCall.enqueue(callback);
    }

    public void getContentBean(String path, HashMap<String, String> params, final HttpCallBack<ContentBean> contentBeanHttpCallBack) {
        Call<ContentBean> contentBeanCall = RetrofitHelper
                .getInstance().createRetrofitService(IHomeService.class)
                .getContentBean(path, params);
        contentBeanCall.enqueue(new Callback<ContentBean>() {
            @Override
            public void onResponse(Call<ContentBean> call, Response<ContentBean> response) {
                ContentBean contentBean = response.body();
                if (contentBean != null) {
                    contentBeanHttpCallBack.onSucess(contentBean);
                }
            }

            @Override
            public void onFailure(Call<ContentBean> call, Throwable t) {
                Log.i("TAG", "" + t.getMessage());
                contentBeanHttpCallBack.onFail();
            }
        });
    }

    public void getSearchHintBean(final HttpCallBack<SearchHintBean> hintBeanHttpCallBack) {
        Call<SearchHintBean> searchHintBeanCall = RetrofitHelper
                .getInstance()
                .createRetrofitService(IHomeService.class)
                .getSearchHintBean();
        searchHintBeanCall.enqueue(new Callback<SearchHintBean>() {
            @Override
            public void onResponse(Call<SearchHintBean> call, Response<SearchHintBean> response) {
                SearchHintBean searchHintBean = response.body();
                hintBeanHttpCallBack.onSucess(searchHintBean);
            }

            @Override
            public void onFailure(Call<SearchHintBean> call, Throwable t) {
                hintBeanHttpCallBack.onFail();
            }
        });
    }
    public void getProductBean(String path, final HttpCallBack<ProductBean> productBeanHttpCallBack) {
        Call<ProductBean> productBeanCall = RetrofitHelper
                .getInstance()
                .createRetrofitService(IHomeService.class)
                .getProductBean(path);
        productBeanCall.enqueue(new Callback<ProductBean>() {
            @Override
            public void onResponse(Call<ProductBean> call, Response<ProductBean> response) {
                productBeanHttpCallBack.onSucess(response.body());
            }
            @Override
            public void onFailure(Call<ProductBean> call, Throwable t) {
                productBeanHttpCallBack.onFail();
            }
        });
    }

    // todo:得到限时特卖详情的内容
    public void getOutletsDetailResultBean(String id, String pager, final Map<String, String> params, Callback<OutletsDeatailsBean> callback) {
        Call<OutletsDeatailsBean> outletsDeatailsBeanCall = RetrofitHelper
                .getInstance()
                .createRetrofitService(IHomeService.class)
                .getDetailsBean(id, pager, params);
        outletsDeatailsBeanCall.enqueue(callback);

    }

    // todo:得到限时特卖购物详情的的内容
    public void getOutletProductBean(String productId, final Map<String, String> params, Callback<OutletsProductBean> callback) {
        Call<OutletsProductBean>outletsProductBeanCall = RetrofitHelper
                .getInstance()
                .createRetrofitService(IHomeService.class)
                .getIproductBean(productId, params);
        outletsProductBeanCall.enqueue(callback);

    }


}


/*  @Override
            public void onResponse(Call<HotShowEntity> call, Response<HotShowEntity> response) {
                HotShowEntity entity = response.body();
                for (MovieEntity movieEntity : entity.getList()) {
                    Log.d("-----", "" + movieEntity.getName());
                }
                entityHttpCallBack.onSucess(response.body());
            }

            @Override
            public void onFailure(Call<HotShowEntity> call, Throwable t) {
                entityHttpCallBack.onFail();
            }*/

